package com.homedo.odin.solution.service.bizservice.mng;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.ClassifyDataResp;
import com.homedo.odin.solution.contract.mng.CommonTypeDataReq;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.enumcode.basic.IsUseEnum;
import com.homedo.odin.solution.model.dto.ClassifyDataResponseBean;
import com.homedo.odin.solution.model.entity.CommonTypeData;
import com.homedo.odin.solution.model.entity.Specification;
import com.homedo.odin.solution.service.CommonTypeDataServiceImpl;
import com.homedo.odin.solution.service.SpecificationServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhaoxudong
 * @date: 2018/4/10 14:53
 * @Description:
 */
@Service
public class MngStandardTypesBizService {

    @Autowired
    CommonTypeDataServiceImpl commonTypeDataService;
    @Autowired
    SpecificationServiceImpl specificationService;

    public ClassifyDataResp list(Integer firstCassify, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<CommonTypeData> queryResult = new PageInfo<>(
                commonTypeDataService.querySpecificationclassifyList(firstCassify));
        List<CommonTypeData> list = queryResult.getList();
        List<ClassifyDataResponseBean> resultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            ClassifyDataResponseBean response;
            for (CommonTypeData commonTypeData : list) {
                response = new ClassifyDataResponseBean();
                response.setId(commonTypeData.getId());
                response.setName(commonTypeData.getName());
                response.setParentId(commonTypeData.getParentId());
                response.setLastModifiedTime(commonTypeData.getLastModifiedTime());
                CommonTypeData commonData = commonTypeDataService.getCommonTypeDataById(commonTypeData.getParentId());
                if (commonData != null) {
                    response.setParentName(commonData.getName());
                }
                //是否在使用
                List<Specification> specificationList = specificationService
                        .querySpecificationListByClassify(response.getId());
                if (CollectionUtils.isNotEmpty(specificationList)) {
                    response.setIsUse(IsUseEnum.YES.getDbValue());
                } else {
                    response.setIsUse(IsUseEnum.NO.getDbValue());
                }
                resultList.add(response);
            }
        }
        ClassifyDataResp resp = BeanUtils.transfrom(ClassifyDataResp.class, queryResult);
        resp.setList(resultList);
        return resp;
    }

    public void add(CommonTypeDataReq commonTypeDataRequest) {
        if (!NumberHelper.validate(commonTypeDataRequest.getParentId()) ||
                StringUtils.isEmpty(commonTypeDataRequest.getName())) {
            throw new BizException(Meta.CODE_451.getCode(), "参数不正确！");
        }
        CommonTypeData addcommonTypeData = new CommonTypeData();
        if (commonTypeDataRequest.getParentId() != 0) {
            CommonTypeData commonTypeData = commonTypeDataService
                    .getCommonTypeDataById(commonTypeDataRequest.getParentId());
            if (commonTypeData == null) {
                throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "关联的父亲节点不存在!");
            }
            List<CommonTypeData> dataList = commonTypeDataService
                    .queryDataByName(commonTypeDataRequest.getName(), commonTypeDataRequest.getParentId());
            if (CollectionUtils.isNotEmpty(dataList)) {
                throw new BizException(ErrorCodeEnum.IsExist.getDbValue(), "该分类名称已存在!");
            }
            addcommonTypeData.setName(commonTypeDataRequest.getName());
            addcommonTypeData.setParentId(commonTypeDataRequest.getParentId());
            Integer id = commonTypeDataService.addCommonTypeData(addcommonTypeData);
            addcommonTypeData.setId(id);
            addcommonTypeData.setDepth(commonTypeData.getDepth() + 1);
            addcommonTypeData.setPath(String.format("%s,%s", commonTypeData.getPath(), id));
        } else {
            addcommonTypeData.setName(commonTypeDataRequest.getName());
            addcommonTypeData.setParentId(commonTypeDataRequest.getParentId());
            Integer id = commonTypeDataService.addCommonTypeData(addcommonTypeData);
            addcommonTypeData.setId(id);
            addcommonTypeData.setDepth(1);
            addcommonTypeData.setPath(id.toString());
        }
        commonTypeDataService.updateCommonTypeData(addcommonTypeData);
    }

    public void edit(CommonTypeDataReq commonTypeDataRequest) {
        if (!NumberHelper.validate(commonTypeDataRequest.getId()) ||
                StringUtils.isEmpty(commonTypeDataRequest.getName())) {
            throw new BizException(Meta.CODE_451.getCode(), "参数不正确！");
        }
        CommonTypeData data = commonTypeDataService.getCommonTypeDataById(commonTypeDataRequest.getId());
        if (data == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该分类信息不存在!");
        }
        List<CommonTypeData> dataList = commonTypeDataService
                .queryDataByName(commonTypeDataRequest.getName(), data.getParentId());
        if (CollectionUtils.isNotEmpty(dataList)) {
            throw new BizException(ErrorCodeEnum.IsExist.getDbValue(), "该分类名称已存在!");
        }
        data.setName(commonTypeDataRequest.getName());
        commonTypeDataService.updateCommonTypeData(data);
    }

    public void del(Integer id) {
        if (!NumberHelper.validatePositive(id)) {
            throw new BizException(Meta.CODE_451.getCode(), "参数错误");
        }
        CommonTypeData commonTypeData = commonTypeDataService.getCommonTypeDataById(id);
        if (commonTypeData == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该节点不存在!");
        }
        //删除该节点以及它下面所有的子节点
        List<CommonTypeData> childList = commonTypeDataService.queryChildNodeById(id);
        if (CollectionUtils.isNotEmpty(childList)) {
            throw new BizException(ErrorCodeEnum.NotSupport.getDbValue(), "该节点为父节点无法删除!");
        }
        //
        //是否在使用
        List<Specification> specificationList = specificationService.querySpecificationListByClassify(id);
        if (CollectionUtils.isNotEmpty(specificationList)) {
            throw new BizException(ErrorCodeEnum.NotSupport.getDbValue(), "该分类正在被使用，无法删除!");
        }
        commonTypeDataService.deleteCommonDataById(commonTypeData);
    }
}
